package study.springboot.mvc.auth;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TraceKey {

//    private static final  ThreadLocal<String> LOCAL = new ThreadLocal<>(){
//    };
//    private static final InheritableThreadLocal<String> LOCAL = new InheritableThreadLocal<>(){
//    };

    private static final TransmittableThreadLocal<String> LOCAL = new TransmittableThreadLocal<String>() {

        @Override
        protected String initialValue() {
            return super.initialValue();
        }

        @Override
        protected void beforeExecute() {
            log.info("before execute");
        }

        @Override
        protected void afterExecute() {
            log.info("after execute");
        }

        @Override
        protected String childValue(String parentValue) {
            return super.childValue(parentValue);
        }
    };

    /**
     * 设置Trace Key
     */
    public static void set(String traceKey) {
        LOCAL.set(traceKey);
    }

    /**
     * 获取Trace Key
     */
    public static String get() {
        return LOCAL.get();
    }

    /**
     * 移除Trace Key
     */
    public static void remove() {
        LOCAL.remove();
    }
}
